package com.example.bookkeep.login;

import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bookkeep.R;
import com.example.bookkeep.Utils.URLUtils;
import com.example.bookkeep.Utils.OkHttpUtil;

import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.*;

public class ForgotPasswordActivity extends AppCompatActivity {

    private EditText etNewPassword, etConfirmPassword;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_password);

        username = getIntent().getStringExtra("username");

        etNewPassword = findViewById(R.id.etNewPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        Button btnSubmitReset = findViewById(R.id.btnSubmitReset);

        btnSubmitReset.setOnClickListener(v -> {
            String newPassword = etNewPassword.getText().toString().trim();
            String confirmPassword = etConfirmPassword.getText().toString().trim();

            if (newPassword.isEmpty() || confirmPassword.isEmpty()) {
                Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
                return;
            }

            if (!newPassword.equals(confirmPassword)) {
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            new Thread(() -> {
                try {
                    JSONObject json = new JSONObject();
                    json.put("username", username);
                    json.put("password", newPassword);
                    json.put("newPassword", newPassword); // API 要求

                    RequestBody body = RequestBody.create(json.toString(), MediaType.parse("application/json"));
                    Request request = new Request.Builder()
                            .url(URLUtils.BASE_URL + "/user/resetPassword")
                            .post(body)
                            .build();

                    Response response = OkHttpUtil.getInstance().newCall(request).execute();
                    if (response.isSuccessful()) {
                        JSONObject result = new JSONObject(response.body().string());
                        int code = result.getInt("code");
                        runOnUiThread(() -> {
                            if (code == 1) {
                                Toast.makeText(this, "密码重置成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(ForgotPasswordActivity.this, LoginActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                                finish(); // 关闭当前页面

                            } else {
                                try {
                                    Toast.makeText(this, "重置失败：" + result.getString("message"), Toast.LENGTH_SHORT).show();
                                } catch (JSONException e) {
                                    throw new RuntimeException(e);
                                }
                            }
                        });
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        });
    }
}
